package jubaozhu.controller.admin;

import com.github.pagehelper.PageInfo;
import jubaozhu.domain.User;
import jubaozhu.extra.base.AdminBaseController;
import jubaozhu.extra.pojo.UserPo;
import jubaozhu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import zhibi.commons.exception.MessageException;
import zhibi.frame.domain.Page;
import zhibi.frame.mybatis.example.Example;
import zhibi.frame.mybatis.example.ExampleType;

@Controller
@RequestMapping("admin/user")
public class AdminUserController extends AdminBaseController {

    @Autowired
    private UserService userService;

    /**
     * 列表
     *
     * @param model
     * @param page
     * @return
     */
    @RequestMapping("list")
    public String list(Model model, Page page, UserPo userPo,String starttime,String endtime) {
        Example example = Example.getInstance()
                .addParam("u.phone", userPo.getPhone(), ExampleType.Operation.LIKE)
                .addParam("u.status", userPo.getStatus())
                .addParam("u.addtime",starttime, ExampleType.Operation.GTE)
                .addParam("u.addtime",endtime, ExampleType.Operation.LTE)
                .addOrder("u.addtime", ExampleType.OrderType.DESC);
        PageInfo<UserPo> pageInfo = userService.selectPoByExample(example, page);
        setModelAttribute(model, pageInfo);
        return "admin/user/list";
    }

    /**
     * 更新状态
     *
     * @param id
     * @return
     */
    @RequestMapping("updateStatus/{id}")
    public String updateStatus(@PathVariable Integer id) {
        User user = userService.selectByPrimaryKey(id);
        if (null == user) throw new MessageException("该用户不存在");
        user.setStatus((user.getStatus() + 1) % 2);
        user.setUserid(sessionAdmin().getId());
        userService.updateByPrimaryKeySelective(user);
        return refresh();
    }
}
